﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;

namespace RC1.student
{
    public partial class Student : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user_id"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            if (!Session["user_level"].Equals("0"))
            {
                Response.Redirect("../Default.aspx");
            }
            populateMenu();
            populateLoggedinLabel();
        }

        private void populateLoggedinLabel()
        {
            int id = Convert.ToInt32(Session["user_id"]);
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var user = (from ui in db.user_info
                        where ui.user_id == id
                        select ui).SingleOrDefault();
            Label1.Text = "<p>Innritaður sum næmingur:<br/>" + user.user_info_first_name + " " + user.user_info_last_name + "</p>";
        }
        private void populateMenu()
        {
            int id = Convert.ToInt32(Session["user_id"].ToString());
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var result = (from r in db.attendings
                          join c in db.classes on r.class_id equals c.class_id
                          where r.user_id == id
                          orderby c.class_name ascending
                          select new
                          {
                              class_name = c.class_name,
                              class_id = c.class_id
                          });

            menuLabel.Text = "<div id='menu'>";
            menuLabel.Text += "<ul>";
            menuLabel.Text += "<li><a href='Default.aspx'>Heim</a></li>";
            foreach (var row in result)
            {
                menuLabel.Text += "<li><a href='viewClass.aspx?id=" + row.class_id + "'>" + row.class_name + "</a></li>";

            }
            menuLabel.Text += "<li><a href='../logout.aspx' target='_parent'>Útrita</a></li>";
            menuLabel.Text += "</ul>";
            menuLabel.Text += "</div>";
        }
    }
}